<template>
    <div class="manage">
        <el-dialog
            title="提示"
            :visible.sync="dialogVisible"
            width="50%"
            :before-close="handleClose">
<!--            用户的表单信息-->
            <el-form ref="form" :rules="rules" :inline="true" :model="form" label-width="80px">
                <el-form-item label="姓名" prop="name">
                    <el-input placeholder="Please input your name" v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="年龄" prop="age">
                    <el-input placeholder="Please input your age" v-model="form.age"></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="sex">
                    <el-select v-model="form.sex" placeholder="Please select">
                        <el-option label="male" value="1"></el-option>
                        <el-option label="female" value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="出生日期" prop="birthday">
                    <el-date-picker type="date" placeholder="选择日期" v-model="form.birthday"
                                    style="width: 100%;" value-format="yyyy-MM-DD"></el-date-picker>
                </el-form-item>
                <el-form-item label="地址" prop="addr">
                    <el-input placeholder="Please input your addr" v-model="form.addr"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="submit">确 定</el-button>
            </span>
        </el-dialog>
        <div class="manage-header">
            <el-button @click="handleAdd" type="primary">
                + 新增
            </el-button>
            <el-table
                :data="tableData"
                style="width: 100%">
                <el-table-column
                    prop="name"
                    label="姓名">
                </el-table-column>
                <el-table-column
                    prop="age"
                    label="年龄">
                </el-table-column>
                <el-table-column
                    prop="sex"
                    label="性别">
                    <template slot-scope="scope">
                        <span>{{ scope.row.sex === 1 ? '男' : '女'}}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="birth"
                    label="出生日期">
                </el-table-column>
                <el-table-column
                    prop="addr"
                    label="地址">
                </el-table-column>
                <el-table-column
                        label="操作">
                    <template slot-scope="scope">
                        <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
                        <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
    </div>
</template>

<script>
import { getUser, addUser, editUser, delUser } from  '../api'
export default {
    name: 'UserTest',
    data() {
        return {
            dialogVisible: false,
            form: {
                name: '',
                age: '',
                sex: '',
                birthday: '',
                addr: ''
            },
            rules: {
                name: [
                    {required: true, message: '请输入姓名'}
                ],
                age: [
                    {required: true, message: '请输入age'}
                ],
                sex: [
                    {required: true, message: '请输入sex'}
                ],
                birthday: [
                    {required: true, message: '请输入birthday'}
                ],
                addr: [
                    {required: true, message: '请输入addr'}
                ]
            },
            tableData: [],
            modelType: 0 // 0 represent add, 1 represent edit
        }
    },
    mounted() {
        this.getList()
    },
    methods: {
        getList() {
            getUser().then(( {data} ) => {
                console.log(data)
                this.tableData = data.list
            })
        },
        // 提交用户表单
        submit() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    if(this.modelType === 0) {
                        console.log(this.form)
                        addUser(this.form).then(() => {
                            // 插入完成后,重新获取列表
                            this.getList()
                        })
                    }else {
                        editUser(this.form).then(() => {
                            // 插入完成后,重新获取列表
                            this.getList()
                        })
                    }
                    // 通过校验
                    console.log(this.form)
                    // form 表单数据clear
                    this.$refs.form.resetFields()
                    // 关闭弹窗
                    this.dialogVisible = false
                }
            })
        },

        // 弹窗关闭的时候触发该函数
        handleClose() {
            this.$refs.form.resetFields()
            this.dialogVisible = false
        },
        cancel() {
            this.handleClose()
        },
        handleDelete(row) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                delUser( {id : row.id }).then(() => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getList()
                })

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        handleEdit(row) {
            this.modelType = 1
            this.dialogVisible = true
            // 对当前行数据进行深拷贝
            this.form = JSON.parse(JSON.stringify(row))
        },
        handleAdd() {
            this.modelType = 0
            this.dialogVisible = true
        }
    }
}
</script>
